import { NextRequest, NextResponse } from 'next/server';
import { WechatService } from '@/services/wechat';
import { getEnvConfig, getServiceUrl } from '@/utils/env';

export async function POST(request: NextRequest) {
  try {
    const { path, env, type } = await request.json();
    console.log('API called with:', { path, env, type });

    const wechatService = WechatService.getInstance();
    console.log('WechatService instance created');

    const qrCodeBuffer = await wechatService.getQRCode({ path, env, type });
    console.log('QR code generated');

    // 将 Buffer 转换为 Blob
    const blob = new Blob([qrCodeBuffer], { type: 'image/png' });

    return new NextResponse(blob, {
      headers: {
        'Content-Type': 'image/png',
      },
    });
  } catch (error) {
    console.error('Error generating QR code:', error);
    return NextResponse.json({ error: 'Failed to generate QR code' }, { status: 500 });
  }
}
